package com.foreknow.demo13;


import java.io.*;


/**
 * ObjectOutputStream 对象输出流，可以向文件中写对象
 * ObjectInputStream 对象输入流，可以读取文件中的对象
 */
public class ReadObject {
    public static void main(String[] args) {
        try {
            //向文件中写对象
//          FileOutputStream fos = new FileOutputStream("data.ser");
//          ObjectOutputStream oos = new ObjectOutputStream(fos);
//          oos.writeObject(new Employee("张三",38,"市场部"));
//          oos.writeObject(new Employee("李四",48,"开发部"));
//          oos.close();

            //读取文件中的对象信息

            FileInputStream fis = new FileInputStream("data.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Employee e1 = (Employee) ois.readObject();
            Employee e2 = (Employee) ois.readObject();
            e1.showInfo();
            e2.showInfo();
            ois.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
